package net.wanho.shop.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import net.wanho.shop.po.Member;
import net.wanho.shop.service.MemberService;
import net.wanho.shop.vo.AjaxResult;
import net.wanho.shop.vo.PageVo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;


/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author 猛虎王
 * @since 2024-12-24
 */
@Api(tags = "03.会员表接口")
@RestController
@RequestMapping("/members")
@RequiredArgsConstructor
public class MemberController {

    private final MemberService memberService;

    @ApiOperation("会员表分页查询")
    @GetMapping
    public AjaxResult findPage(@ApiParam("页码") @RequestParam(defaultValue = "1") int pageNum,
                               @ApiParam("页大小") @RequestParam(defaultValue = "5") int pageSize,
                               @ApiParam("查询条件") Member member) {
        PageVo pageVo = memberService.findPageByMember(pageNum, pageSize, member);
        return AjaxResult.success(pageVo);
    }

    @ApiOperation("根据主键查询会员表")
    @GetMapping("/{id}")
    public AjaxResult findById(@PathVariable Serializable id) {
        return AjaxResult.success(memberService.findById(id));
    }

    @ApiOperation("新增会员表")
    @PostMapping
    public AjaxResult add(@RequestBody Member member) {
        memberService.add(member);
        return AjaxResult.success();
    }

    @ApiOperation("修改会员表")
    @PutMapping("/{id}")
    public AjaxResult modify(@RequestBody Member member, @PathVariable Integer id) {
        memberService.modifyById(member, id);
        return AjaxResult.success();
    }

    @ApiOperation("根据主键删除会员表")
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable Serializable id) {
        memberService.removeById(id);
        return AjaxResult.success();
    }

    @ApiOperation("批量删除会员表")
    @DeleteMapping
    public AjaxResult removeByIds(@RequestBody ArrayList<Integer> ids) {
        memberService.removeByIdList(ids);
        return AjaxResult.success();
    }

    @SneakyThrows
    @ApiOperation("会员导出")
    @GetMapping("/export")
    public void export(@ApiParam("页码") @RequestParam(defaultValue = "1") int pageNum,
                       @ApiParam("页大小") @RequestParam(defaultValue = "5") int pageSize,
                       @ApiParam("查询条件") Member member, HttpServletResponse response) {
        Workbook workbook = memberService.export(pageNum, pageSize, member);

        response.setContentType("application/vnd.ms-excel");  // 设置响应类型 MIME 类型

        workbook.write(response.getOutputStream());
    }

    @SneakyThrows
    @ApiOperation("会员导入")
    @PostMapping("/import")
    public AjaxResult importExcel(@RequestBody MultipartFile multipartFile) {
        memberService.importExcel(multipartFile);
        return AjaxResult.success();
    }
}
